import { isEmpty, deepClone } from "@/util/common"
import {mergeOptions} from "../core/pageOption";
/**
 * 一个用于管理分页列表状态和交互的模块
 * 提供上拉加载、重置、错误处理等功能
 * @module PageListManager
 */
export function usePageListManager(
    {
      config = {},
      getList = () => {
      }
    }) {
  /**
   * 分页状态对象
   */
  const paginationState = ref({
    isRefreshing: false,
    isLoading: false,
    isError: false,
    isFinished: false,
    displayType: 0, // 显示状态：0 - 默认, 1 - 数据为空, 2 - 无更多数据, 3 - 不显示无更多数据
  });
  
  /**
   * 分页配置对象
   * @type {{errorText: string, finishedText: string, pageSize: number, offset: number}}
   */
  const paginationConfig = ref(mergeOptions(config, 'upOption'));
  const pageNo = ref(paginationConfig.pageNo ?? 1);
  const initPageNo = ref(pageNo.value);
  
  /**
   * 重置上拉滚动状态
   */
  const resetUpScroll = () => {
    Object.assign(paginationState.value, {
      isFinished: false,
      isError: false,
      isRefreshing: false,
      isLoading: true,
      displayType: 0,
    });
    
    pageNo.value = initPageNo.value;
    upCallback();
  };
  
  /**
   * 上拉加载回调
   * @async
   */
  const upCallback = async () => {
    paginationState.value.isLoading = true;
    getList(pageNo.value, paginationConfig.value.pageSize);
  };
  
  /**
   * 上拉加载失败回调
   */
  const upCallbackError = () => {
    if (paginationState.value.isRefreshing) {
      paginationState.value.isRefreshing = false;
    }
    paginationState.value.isError = true;
    paginationState.value.isLoading = false;
    saveDisplayType();
  };
  
  /**
   * 上拉加载成功回调
   * @param {number} [num=0] 加载的数据量
   * @param {boolean|null} [hasNextPage=null] 是否有下一页数据
   * @param {number} [time=300] 动画延迟时间
   * @returns {Promise<void>} 异步操作，无返回值
   */
  const upCallbackSuccess = (num = 0, hasNextPage = null, time = 300) => {
    setTimeout(() => {
      if (paginationState.value.isRefreshing) {
        paginationState.value.isRefreshing = false;
      }
      paginationState.value.isLoading = false;
      if (!hasNextPage || (hasNextPage === null && num < paginationConfig.value.pageSize)) {
        saveDisplayType(num);
        paginationState.value.isFinished = true;
        return;
      }
      
      pageNo.value += 1;
    }, time);
  };
  
  /**
   * 保存显示类型
   * @param {number} [num=0] 数据量
   */
  const saveDisplayType = (num = 0) => {
    if (initPageNo.value !== pageNo.value) {
      paginationState.value.displayType = 2;
      return;
    }
    if (num === 0) {
      paginationState.value.displayType = 1;
      return;
    }
    
    paginationState.value.displayType = 2;
  };
  
  /**
   * 设置分页配置项
   * @param {string} key 配置项键
   * @param {*} val 配置项值
   */
  const savePaginationConfig = (val) => {
    paginationConfig.value = mergeOptions(val, 'upOption')
    const newInitPageNo = paginationConfig.value.pageNo ?? 1
    if (initPageNo.value === newInitPageNo) {
      return
    }
    initPageNo.value = newInitPageNo
  };
  
  return {
    paginationState,
    paginationConfig,
    resetUpScroll,
    upCallback,
    upCallbackError,
    upCallbackSuccess,
    savePaginationConfig,
  };
}
